package beans;

import java.util.Collections;
import java.util.Map;
import java.util.TreeMap;

import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.faces.bean.ApplicationScoped;
import javax.faces.bean.ManagedBean;

import controller.PrinterTimerBeanRemote;
import dtos.UserDTO;

/**
 * Java bean that contains the map of online users, mapped by the ip of the tech resource that they are logged in at.
 * 
 */

@ManagedBean(name="onlineUsers")
@ApplicationScoped
public class OnlineUsers{
	private Map<String, UserDTO> onlineMap;

	@EJB
	private PrinterTimerBeanRemote printerTimer;
	
	@PostConstruct
	public void init() {
		printerTimer.startTimer("First Timer");
	}
	
	public OnlineUsers() {
		onlineMap = Collections.synchronizedMap(new TreeMap<String, UserDTO>());
	}
	
	public Map<String, UserDTO> getOnlineMap() {
		return onlineMap;
	}

	public void setOnlineMap(Map<String, UserDTO> onlineMap) {
		this.onlineMap = onlineMap;
	}
	
	/**
	 * Insert a newly logged in user to the map of online users.
	 * 
	 * @param user - the user to be added to the map
	 */
	public void addUserToMonitor(UserDTO user) {
		onlineMap.put(user.getIp(), user);
	}
	
	/**
	 * Removes a user from the list on logged in users.
	 * 
	 * @param user - the user to be removed from the map
	 */
	public void removeUserFromMonitor(UserDTO user){
		onlineMap.remove(user.getIp());
	}
}
